package org.mipt.infsec.steganography.gui;

import javax.swing.filechooser.FileFilter;
import java.io.File;

/**
 * Created by IntelliJ IDEA.
 * User: User
 * Date: 15.12.11
 * Time: 12:29
 * To change this template use File | Settings | File Templates.
 */
class ImageFilter extends FileFilter {

	@Override
	public boolean accept(File f) {
		if (f.isDirectory()) {
			return true;
		}
		return isGifExtension(f);
	}

	@Override
	public String getDescription() {
		return "Images";
	}

	public static boolean isGifExtension(File f) {
		String ext = null;
		String s = f.getName();
		int i = s.lastIndexOf('.');

		if (i > 0 &&  i < s.length() - 1) {
			ext = s.substring(i+1).toLowerCase();
		}

		if (ext != null) {
			if (ext.equalsIgnoreCase("png")) {
				return true;
			}
		}
		return false;
	}

	public static File addExtension(File f) {
		if (!ImageFilter.isGifExtension(f)) {
			String n = f.getName().concat(".png");
			f = new File(f.getParentFile(), n);
		}
		return f;
	}

}
